//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Web.Mail;
using CommunityServer.Components;
using CommunityServer.Mail.Common.Components;

namespace CommunityServer.Discussions.Components
{



	/// <summary>
	/// Summary description for ForumEmails.
	/// </summary>
	public class ForumEmails : Emails
	{
		private ForumEmails(){}

		private static readonly string PrivateMessageNotification = "PrivateMessageNotification";
		private static readonly string NewMessagePostedToForum = "NewMessagePostedToForum";

		#region ForumTracking



		/// <summary>
		/// Internal method used to return a message to SectionTracking which will add the message to the Queue
		/// </summary>
		/// <param name="post"></param>
		/// <param name="subscriber"></param>
		/// <returns></returns>
		private static MailMessage ForumCreatePost(Post post, User subscriber, FormatList formats)
		{
			if (post.SectionID == 0)
				return CreatePost(post, PrivateMessageNotification, subscriber, null, null, false, subscriber.EnableHtmlEmail);
			else
			{
				if (CSContext.Current.SiteSettings.EnableMailGateway)
				{
					MailingList list = MailingLists.GetMailingList(post.SectionID);
					if((list != null) && list.IsMailingList)
						return null;
					else
						return CreatePost(post, NewMessagePostedToForum, subscriber, null, null, false, subscriber.EnableHtmlEmail);
				}
				else
					return CreatePost(post, NewMessagePostedToForum, subscriber, null, null, false, subscriber.EnableHtmlEmail);
			}
		}
		
		// *********************************************************************
		//  ForumTracking
		//
		/// <summary>
		/// This method sends an email to all of those people who have subscribed
		/// to track a particular forum.  This function is called when a new
		/// message is added to the forum.
		/// <seealso cref="SendEmail"/>
		/// </summary>
		/// <param name="postID">The ID of the newly posted message.</param>
		/// <remarks>This method first obtains a list of all of those users who are
		/// subscribed to track the forum that the new email was added to.  It then
		/// calls SendEmail, passing along this information.</remarks>
		/// 
		// ********************************************************************/
		public static void ForumTracking (Post post) 
		{
			if (post == null)
				return;

			if(CSContext.Current.SiteSettings.EnableMailGateway)
			{
				MailingList list = MailingLists.GetMailingList(post.SectionID);
				if(list != null && list.IsMailingList)
					return;
			}

			//ScottW: Refactored subscription logic to base SectionTracking and to use
			//the Enque delegate
			SectionTracking(post,new EnquePostEmails(ForumCreatePost),null);

			#region Old
//			Hashtable threadSubscribers = GetEmailsTrackingThread(post.PostID);
//			Hashtable forumSubscribers = GetEmailsTrackingSectionByPostID(post.PostID);
//
//
//			foreach (User threadSubscriber in threadSubscribers) 
//			{
//				// Make sure we don't send an email to the user that posted the message
//				//			
//				if (threadSubscriber.Email != post.User.Email) 
//				{
//
//					// test for PM message
//					//
//                    
//					if (post.SectionID == 0)
//						EnqueuEmail(CreatePost(post, PrivateMessageNotification, threadSubscriber.Email, null, null, false, threadSubscriber.EnableHtmlEmail));				
//					else
//						EnqueuEmail(CreatePost(post, NewMessagePostedToForum, threadSubscriber.Email, null, null, false, threadSubscriber.EnableHtmlEmail));				
//				}
//
//				// Make sure we don't send duplicates to forum subscribers
//				//
//				// TDD 5/24/2004 FRMS-33
//				// old code was removing elements from the same collection that was being enumerated. Illegal operation.
////				for( int u = 0; u < forumSubscribers.Count; u++ ) 
////				{
////					
////					User forumSubscriber = forumSubscribers[u] as User;
////					if( forumSubscriber			!= null
////						&&	forumSubscriber.Email	== threadSubscriber.Email ) 
////					{
////						forumSubscribers.RemoveAt(u--);
////					}
////				}
//
//				if(forumSubscribers.Contains(threadSubscriber.Email.ToLower()))
//					forumSubscribers.Remove(threadSubscriber.Email.ToLower());
//			}
//
//			foreach (User forumSubscriber in forumSubscribers) 
//			{
//				// Make sure we don't send an email to the user that posted the message
//				//			
//				if (forumSubscriber.Email != post.User.Email) 
//				{
//
//					// test for PM message
//					//
//					if (post.SectionID == 0)
//						EnqueuEmail(CreatePost(post, PrivateMessageNotification, forumSubscriber.Email, null, null, false, forumSubscriber.EnableHtmlEmail));
//					else
//						EnqueuEmail(CreatePost(post, NewMessagePostedToForum, forumSubscriber.Email, null, null, false, forumSubscriber.EnableHtmlEmail));
//				}
//			}
			#endregion
		}
		#endregion

		#region NotifyModerators
		public static void NotifyModerators (Post post) 
		{

		}
		#endregion


	}
}
